/*
 * SPI.c
 *
 * Created: 04.10.2012 09:18:52
 *  Author: bardbakk
 */
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/delay.h>

#include "definitions.h"
#include "SPI.h"

void SPI_init()
{
	//Set MOSI, SS and SCK output, all others input 	
	BIT_SET(DDRB, PB0);
	BIT_SET(DDRB, PB1);
	BIT_SET(DDRB, PB2);
	
	//Set SS high
	BIT_SET(PORTB, PB0);
	
	//Enable SPI, Master, set clock rate fck/16
	BIT_SET(SPCR, SPE);
	BIT_SET(SPCR, MSTR);
	BIT_SET(SPCR, SPR0);
	
}

void SPI_MasterTransmit(uint8_t cData)
{	
	/* Start transmission */
	SPDR = cData;
	/* Wait for transmission complete */
	while(!(SPSR & (1<<SPIF)))
	{
		;
	}
}

char SPI_MasterReceive()
{	
	/* Start transmission */
	SPDR = 'N';
	/* Wait for transmission complete */
	while(!(SPSR & (1<<SPIF)))
	{
		;
	}
	return SPDR;
}
